﻿using DNFImagePacks2Manager.Helpers;
using Microsoft.UI.Xaml.Controls;
using System.IO;

namespace DNFImagePacks2Manager.Models
{

    public class NPKItemImage(string fileName, string? NPKPath = null) : NPKItemBase(NamePrefix, fileName, NPKPath ?? LocalStorageHelper.GetImagePath())
    {
        public static readonly string NamePrefix = "图像";

        public override bool IsOfficalName(string fileName)
        {
            return NPKHelper.FilenameCheckImageOfficial(fileName);
        }

        public override bool RenameCheck(Page page, string newName)
        {
            if (IsOfficalName(newName + ".npk"))
            {
                DialogHelper.ShowErrorMessage(page, "文件名校验非法，不要使用官方名称命名补丁");
                return false;
            }

            NPKItemImage? newItem = GetImageNPKItem(newName, this.BasePath);
            if (newItem != null)
            {
                DialogHelper.ShowErrorMessage(page, "文件名已存在，请使用其他名称");
                return false;
            }

            return true;
        }

        // 获取NPKItem对象，自动判断后缀
        public static NPKItemImage? GetImageNPKItem(string name, string NPKPath)
        {
            string filename = name + ".npk";
            NPKItemImage item = new(filename, NPKPath);
            if (File.Exists(item.EnableFile) || File.Exists(item.DisableFile))
            {
                item.UpdateStatus();
                return item;
            }
            filename = name + ".NPK";
            item = new (filename, NPKPath);
            if (File.Exists(item.EnableFile) || File.Exists(item.DisableFile))
            {
                item.UpdateStatus();
                return item;
            }
            return null;
        }
    }
}
